var api = require('../../config/api.js');
var util = require('../../utils/util.js');
var user = require('../../utils/user.js');

var app = getApp();
// pages/login/login.js
Page({
  data: {
    picker: ['供货商','团长'],
    index:'',
    encryptedData:'',
    iv:'',
    rawData:'',
    signature:'',
    tag:false
  },
  goxy(){
    console.log("sada")
    wx.navigateTo({
      url: '/pages/xieyi/xieyi',
    })
  },
  
  select(){
    this.setData({
      tag:!this.data.tag
    })
    console.log(this.data.tag)
  },

  PickerChange(e) {
    console.log(e);
    // var bb=parseInt(a) + parseInt(b)
    this.setData({
      index: e.detail.value
    })
    wx.setStorageSync('role', e.detail.value )
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成

  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  disLogin() {
    console.log('aaa')
   wx.showToast({
     title: '请登录',
     icon:'none'
   })
  },
  // getPhoneNumber: function (e) {
  //   console.log(e)
  // },
  getPhoneNumber: function (e) {
    console.log(e)
    this.setData({
      iv:e.detail.iv,
      encryptedData:e.detail.encryptedData
    })
    if(this.data.index==''){
      wx.showToast({
        title: '请选择用户登录身份',
        icon:"none"
      })
      return false
    }
    let that = this
    let bb = parseInt(that.data.index) + parseInt(1);
    console.log(bb)

        if (e.detail.iv == undefined) {
          app.globalData.hasLogin = false;
        wx.showToast({
          title: '手机号获取失败',
          icon:none
        })
          return;
        }
        // console.log("11111" + that.data.encryptedData)
        user.checkLogin().catch(() => {

          user.loginByWeixin(that.data.encryptedData, that.data.iv, that.data.rawData,that.data.signature, bb).then(res => {
            app.globalData.hasLogin = true;
            console.log("asasasasa" + res.data.mark)
            if (res.data.mark==1){
            wx.redirectTo({
              url: '/pages/index/index',
            })
            }
            if (res.data.mark == 2) {
              wx.redirectTo({
                url: '/pages/index1/index1',
              })
            }
          }).catch((err) => {
            app.globalData.hasLogin = false;
            // util.showErrorToast('微信登录失败2');
            wx.showToast({
              title: '登录失败',
              icon:'none'
            })
          });

        });
   


  }
})